//
//  SEGVisitorAccessCodeDoorCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/5/12.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGVisitorAccessCodeDoorCell.h"
#import "SEGVisitorAccessCodeModel.h"
#import "SEGVisitorAccessDoorModel.h"

static CGFloat titleHeihgt = 44.0;
static CGFloat tapViewHeight = 36.0;
static CGFloat qcodeViewHeight = 282.0;

@interface SEGVisitorAccessCodeDoorCell ()

/**
 白色背景视图
 */
@property (nonatomic, strong)UIView *bgView;

/**
 二维码图片视图
 */
@property (nonatomic, strong)UIImageView *qcodeImageView;

@end

@implementation SEGVisitorAccessCodeDoorCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_bg;
    self.userInteractionEnabled = YES;
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = kColor_W;
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 2;
    bgView.userInteractionEnabled = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).mas_offset(15.0);
        make.right.equalTo(self.contentView).mas_offset(-15.0);
        make.top.equalTo(self.contentView).mas_offset(0);
        make.bottom.equalTo(self.contentView).mas_offset(-15.0).priorityHigh();
    }];
    self.bgView = bgView;

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = kFont_13;
    titleLabel.textColor = kColor_3;
    titleLabel.text = @"已授权门禁";
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView);
        make.left.equalTo(bgView).mas_offset(15);
        make.height.mas_equalTo(titleHeihgt);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView);
        make.right.equalTo(bgView);
        make.top.equalTo(titleLabel.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    line.tag = 99;
}

- (void)updateDoorInfoWithModel:(SEGVisitorAccessCodeModel *)model
{
    if (self.bgView.subviews.count == 2) {//防止重复添加
        __block CGFloat totalHeight = titleHeihgt + 30.0;
        __block NSMutableArray *doorNames = [NSMutableArray array];
        //先处理二维码门 梳理门的个数
        [model.visitorDoors enumerateObjectsUsingBlock:^(SEGVisitorAccessDoorModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.doorType isEqualToString:@"3"]) {//二维码门
                NSArray *doors = [obj.doorName componentsSeparatedByString:@","];
                [doorNames addObjectsFromArray:doors];
                //创建门二维码界面
                [self seg_createDoorQcodeViewWithDoorModel:obj];
                totalHeight += qcodeViewHeight;
            }else {
                [doorNames addObject:obj.doorName];
            }
        }];
        
        [doorNames enumerateObjectsUsingBlock:^(NSString *doorName, NSUInteger idx, BOOL * _Nonnull stop) {
            [self seg_createDoorInfoViewsWithIndex:idx doorName:doorName];
        }];

        totalHeight += (doorNames.count * tapViewHeight);
        if (self.delegate && [self.delegate respondsToSelector:@selector(updateDoorInfoCellWithHeight:)]) {
            [self.delegate updateDoorInfoCellWithHeight:totalHeight];
        }
    }
}

- (void)seg_createDoorQcodeViewWithDoorModel:(SEGVisitorAccessDoorModel *)doorModel
{
    //找到上一个视图
    __block UIView *lastView;
    [self.bgView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag == 99) {
            lastView = obj;
            lastView.tag = 0;
            *stop = YES;
        }
    }];
    
    //二维码和描述背景界面
    UIView *qcodeView = [[UIView alloc] init];
    [self.bgView addSubview:qcodeView];
    qcodeView.tag = 99;
    [qcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom);
        make.left.right.equalTo(self.bgView);
        make.height.mas_equalTo(qcodeViewHeight);
    }];

    //二维码图片
    UIImageView *qcodeImage = [[UIImageView alloc] init];
    [qcodeView addSubview:qcodeImage];
    [qcodeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qcodeView).mas_offset(50);
        make.centerX.equalTo(qcodeView);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(140);
    }];
    NSURL *qcodeUrl = [NSURL URLWithString:doorModel.qrCodeUrl];
    [qcodeImage setImageWithURL:qcodeUrl placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
    
    //提示
    UILabel *tipsLabel = [[UILabel alloc] init];
    tipsLabel.font = kFont_13;
    tipsLabel.textColor = kColor_T;
    tipsLabel.text = @"请在闸机设备上扫描该二维码，进入场所";
    [self.contentView addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qcodeImage.mas_bottom).mas_offset(28);
        make.centerX.equalTo(qcodeView);
    }];

    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = kColor_L2;
    [qcodeView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(qcodeView).mas_offset(15);
        make.right.equalTo(qcodeView).mas_offset(-15);
        make.bottom.equalTo(qcodeView);
        make.height.mas_equalTo(0.5);
    }];
}

- (void)seg_createDoorInfoViewsWithIndex:(NSUInteger)index doorName:(NSString *)doorName
{
    //找到上一个视图
    __block UIView *lastView;
    [self.bgView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag == (100 + index - 1)) {
            lastView = obj;
            *stop = YES;
        }
    }];
    
    UIView *doorInfoView = [[UIView alloc] init];
    doorInfoView.backgroundColor = kColor_W;
    doorInfoView.tag = 100 + index;
    [self.bgView addSubview:doorInfoView];
    [doorInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView);
        make.right.equalTo(self.bgView);
        make.top.equalTo(lastView.mas_bottom);
        make.height.mas_equalTo(tapViewHeight);
    }];

    UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_visitors_doorimg")];
    UIImageView *iconView = [[UIImageView alloc] initWithImage:image];
    [doorInfoView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(doorInfoView).mas_offset(15);
        make.bottom.equalTo(doorInfoView);
        make.width.mas_equalTo(21.0);
        make.height.mas_equalTo(21.0);
    }];
    //门名字
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = kFont_13;
    nameLabel.textColor = kColor_1;
    [doorInfoView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).mas_offset(10);
        make.centerY.equalTo(iconView);
    }];
    
    nameLabel.text = doorName;
}

@end
